﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace XiaoGang.Soft.XGWMS.UI
{
    class CaptionControlButton : CaptionButton
    {
        public CaptionControlButton()
        {
            Cursor = Cursors.Arrow;
        }


        private Color _LightBackColor = Color.Transparent;

        public Color LightBackColor
        {
            get { return _LightBackColor; }
            set { _LightBackColor = value; }
        }

        public override bool AutoSize
        {
            get
            {
                return false;
            }
            set
            {
                base.AutoSize = false;
            }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            //base.OnPaint(e);

            Form frm = FindForm();
            if (frm.GetType().IsSubclassOf(typeof(BaseForm)) || frm.GetType().Equals(typeof(BaseForm)) == true)
            {
                BaseForm mf = frm as BaseForm;

                Brush b = null;

                if (IsActive == true)
                {
                    if (LightBackColor.Equals(Color.Transparent) == true)
                    {
                        b = new SolidBrush(ControlPaint.LightLight( mf.FormStyleColor));
                    }
                    else
                    {
                        b = new SolidBrush(LightBackColor);
                    }
                }
                else
                {
                    b = new SolidBrush(mf.FormStyleColor);
                }


                e.Graphics.FillRectangle(b, ClientRectangle);
                b.Dispose();

                b = new LinearGradientBrush(
                   this.ClientRectangle,
                   GetColorForInt(mf.AlphaArray[0]),
                   GetColorForInt(mf.AlphaArray[mf.AlphaArray.Length - 2]),
                   LinearGradientMode.BackwardDiagonal
                );
                e.Graphics.FillRectangle(b, ClientRectangle);
                b.Dispose();

                DrawContext(e);


                e.Graphics.DrawLine(Pens.Silver, 1, 0, 1, Width - 1);
                e.Graphics.DrawLine(Pens.Black, 0, 0, 0, Width - 1);

                e.Graphics.DrawLine(Pens.Black, 0, Height - 2, Width, Height - 2);
                e.Graphics.DrawLine(Pens.Silver, 0, Height - 1, Width, Height - 1);

                e.Graphics.DrawLine(Pens.Silver, Width - 1, 0, Width - 1, Height);
                e.Graphics.DrawLine(Pens.Black, Width - 2, 0, Width - 2, Height);

                if (IsSelect == true)
                {
                    e.Graphics.DrawRectangle(Pens.Gray, 1, 0, Width - 4, Height - 3);
                }
                
            }
        }

        public virtual void DrawContext(PaintEventArgs e)
        {
            System.Drawing.Font f = new Font(FontFamily.GenericSansSerif, 18, FontStyle.Bold);
            SizeF size = e.Graphics.MeasureString("×", f);
            e.Graphics.DrawString("×", f, Brushes.White, (Width - size.Width) / 2, -6);
        }

        private Color GetColorForInt(int i)
        {
            return Color.FromArgb(127, i, i, i);
        }
    }

}

